package com.yanqu.road.server.manager.activity.soulbone;

import com.yanqu.road.dao.impl.activity.soulbone.CrossSoulBoneNoticeDataDaoImpl;
import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.soulbone.CrossSoulBoneNoticeData;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.pb.activity.SoulBoneProto;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class CrossSoulBoneActivity {

    private ActivityInfo activityInfo;

    //公告
    private List<CrossSoulBoneNoticeData> noticeDataList;

    //活动配置项
    Map<String, ActivityConfig> activityConfigMap;

    private int noticeCount;

    public CrossSoulBoneActivity(ActivityInfo activityInfo) {
        this.activityInfo = activityInfo;
    }


    public void init(){
        List<Integer> activityIdList = new ArrayList<>();
        activityIdList.add(activityInfo.getActivityId());
        activityConfigMap = ActivityBussiness.getActivityConfigMap(activityIdList).get(activityInfo.getActivityId());

        noticeCount = activityConfigMap.get("SOUL_BONE_MARQUEE_LIMIT").getIntValue();

        //数据加载

        //公告
        noticeDataList = new CrossSoulBoneNoticeDataDaoImpl().getCrossSoulBoneNoticeData(activityInfo.getActivityId(), noticeCount);
        Collections.reverse(noticeDataList);
    }

    public void save(){

        for (int i = 0; i < noticeDataList.size(); i++) {
            CrossSoulBoneNoticeData data = noticeDataList.get(i);
            if(data.isInsertOption()){
                new CrossSoulBoneNoticeDataDaoImpl().add(data);
            }else if(data.isUpdateOption()){
                new CrossSoulBoneNoticeDataDaoImpl().update(data);
            }
        }

    }


    public void addDrawNotice(long serverId, long playerId, List<SoulBoneProto.SoulBoneNoticeMsg> noticeList) {
        synchronized (noticeDataList){
            for (SoulBoneProto.SoulBoneNoticeMsg noticeMsg : noticeList) {
                CrossSoulBoneNoticeData noticeData = new CrossSoulBoneNoticeData();
                noticeData.setActivityId(activityInfo.getActivityId());
                noticeData.setGetTime(noticeMsg.getTime());
                noticeData.setServerId(serverId);
                noticeData.setUserId(playerId);
                noticeData.setNickName(noticeMsg.getNickName());
                noticeData.setReward(noticeMsg.getReward());
                noticeData.setParam(noticeMsg.getParams());
                noticeData.setInsertOption();
                noticeDataList.add(noticeData);
            }
            while (noticeDataList.size() > noticeCount){
                noticeDataList.remove(0);
            }
        }

        SoulBoneProto.SoulBoneNoticeSyncMsg.Builder sync = SoulBoneProto.SoulBoneNoticeSyncMsg.newBuilder();
        sync.addAllNotice(noticeList);
        for (Long sid : activityInfo.getServerIdList()) {
            MessageHelper.sendPacket(sid, 0, YanQuMessageUtils.buildMessage(Protocol.S_SOUL_BONE_NOTICE_SYNC, sync));
        }
    }

    public SoulBoneProto.SoulBoneNoticeRespMsg.Builder getNoticeList() {

        SoulBoneProto.SoulBoneNoticeRespMsg.Builder respMsg = SoulBoneProto.SoulBoneNoticeRespMsg.newBuilder();

        List<CrossSoulBoneNoticeData> list = noticeDataList;
        if(list != null){
            for (int i = 0; i < list.size(); i++) {
                SoulBoneProto.SoulBoneNoticeMsg.Builder builder = SoulBoneProto.SoulBoneNoticeMsg.newBuilder();
                CrossSoulBoneNoticeData noticeData = list.get(i);
                builder.setTime(noticeData.getGetTime());
                builder.setReward(noticeData.getReward());
                builder.setServerId(noticeData.getServerId());
                builder.setNickName(noticeData.getNickName());
                builder.setType(0);
                builder.setParams(noticeData.getParam());
                respMsg.addNotice(builder);
            }
        }
        respMsg.setRet(0);
        return respMsg;
    }

}
